package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.BackOrder;
import org.ahuanlien.crm.domain.BillRecord;
import org.ahuanlien.crm.domain.CheckOrder;
import org.ahuanlien.crm.mapper.BackOrderMapper;
import org.ahuanlien.crm.mapper.BillRecordMapper;
import org.ahuanlien.crm.mapper.CheckOrderMapper;
import org.ahuanlien.crm.query.CheckOrderQueryObject;
import org.ahuanlien.crm.service.ICheckOrderService;
import org.ahuanlien.crm.util.PageResult;
import org.ahuanlien.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CheckOrderServiceImpl implements ICheckOrderService{
    @Autowired
    CheckOrderMapper checkOrderMapper;
    @Autowired
    BackOrderMapper backOrderMapper;

    @Autowired
    BillRecordMapper billRecordMapper;

    public int deleteByPrimaryKey(Long id) {
        return checkOrderMapper.deleteByPrimaryKey(id);
    }

    public int insert(CheckOrder record) {
        return checkOrderMapper.insert(record);
    }

    public CheckOrder selectByPrimaryKey(Long id) {
        return checkOrderMapper.selectByPrimaryKey(id);
    }

    public List<CheckOrder> selectAll() {
        return checkOrderMapper.selectAll();
    }

    public int updateByPrimaryKey(CheckOrder record) {
        return checkOrderMapper.updateByPrimaryKey(record);
    }

    public PageResult query(CheckOrderQueryObject qo) {
        int count = checkOrderMapper.queryForCount();
        List<CheckOrder> list = checkOrderMapper.queryForList(qo);
        if(count== 0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        return new PageResult(count,list);
    }
    //退回申请
    public void back(Long id) {
        CheckOrder checkOrder = checkOrderMapper.selectByPrimaryKey(id);
        //把信息存入退回单
        BackOrder backOrder = new BackOrder();
        backOrder.setSn(checkOrder.getSn());
        backOrder.setHolder(checkOrder.getHolder());
        backOrder.setCarNo(checkOrder.getCarNo());
        backOrder.setADate(checkOrder.getADate());
        backOrder.setPDate(new Date());
        backOrder.setYear(checkOrder.getYear());
        backOrder.setOpUser(checkOrder.getOpUser());
        backOrder.setAuditor(UserContext.getCurrentUser());
        backOrder.setTotalAmount(checkOrder.getTotalAmount());
        backOrder.setState(checkOrder.getState());
        backOrder.setItem("查看退回理由");
        backOrderMapper.insert(backOrder);

        //把信息存入单据历史记录
        BillRecord billRecord = new BillRecord();
        billRecord.setSn(backOrder.getSn());
        billRecord.setHolder(backOrder.getHolder());
        billRecord.setCar(backOrder.getCarNo());
        billRecord.setADate(backOrder.getADate());
        billRecord.setPDate(new Date());
        billRecord.setYear(backOrder.getYear());
        billRecord.setOpUser(backOrder.getOpUser());
        billRecord.setAuditor(UserContext.getCurrentUser());
        billRecord.setTotalAmount(backOrder.getTotalAmount());
        billRecord.setState("待修改");
        billRecord.setItem("保险产品明细");
        billRecordMapper.insert(billRecord);

        checkOrderMapper.deleteByPrimaryKey(id);
    }

    //审核通过
    public void check(Long id) {
        CheckOrder checkOrder = checkOrderMapper.selectByPrimaryKey(id);

        Integer year = checkOrder.getYear();
        Calendar c = Calendar.getInstance();

        //把已审核待缴费状态的数据保存到单据历史记录
        BillRecord billRecord = new BillRecord();
        billRecord.setSn(checkOrder.getSn());
        billRecord.setHolder(checkOrder.getHolder());
        billRecord.setCar(checkOrder.getCarNo());

        billRecord.setADate(checkOrder.getADate());
        billRecord.setPDate(c.getTime());//核保时间
        /*c.setTime(checkOrder.getPDate());*/
        c.add(Calendar.YEAR,year);//核保时间+保险时长=保险截止日
        billRecord.setYear(checkOrder.getYear());//保险时长
        billRecord.setDDate(c.getTime());

        billRecord.setOpUser(checkOrder.getOpUser());
        billRecord.setAuditor(UserContext.getCurrentUser());
        billRecord.setTotalAmount(checkOrder.getTotalAmount());
        billRecord.setState("已审核待缴费");
        billRecord.setItem("保险产品明细");
        billRecordMapper.insert(billRecord);
        checkOrderMapper.deleteByPrimaryKey(id);
    }
}
